﻿using System;
using System.Collections.Generic;

namespace HYRElectric.PowerSystem
{
	public class CStandardManual
	{
		public static bool GetWireSAndA(string szType, ushort nNum, double dIc, ref double dS, ref double dIs)
		{
			if (szType != "" && nNum > 0 && dIc > 0.0)
			{
				string safeSql = string.Concat(new object[]
				{
					"select wireampacity from wireampacity where wiretypeid =(select id  from wiretype where wiretype ='",
					szType,
					"') and wirenum = ",
					nNum,
					" and wireampacity > ",
					dIc,
					"  order by wireampacity asc limit 0,1"
				});
				dIs = DBhelper.GetReader(safeSql);
				string safeSql2 = string.Concat(new object[]
				{
					"select wiresection from wirestdsection where id = (select wiresectionid from wireampacity where wiretypeid =(select id  from wiretype where wiretype ='",
					szType,
					"') and wirenum = ",
					nNum,
					" and wireampacity = ",
					dIs,
					")"
				});
				dS = DBhelper.GetReader(safeSql2);
				return dIs != -1.0 && dS != -1.0;
			}
			return false;
		}

		public static bool GetPipeDia(string szType, ushort nNum, double dS, string pipeType, ref double dPipeDia)
		{
			if (szType != "" && nNum > 0 && dS > 0.0)
			{
				string safeSql = string.Concat(new object[]
				{
					"select pipedia from wireprotectionpipe where wiretypeid =(select id  from wiretype where wiretype ='",
					szType,
					"') and wirenum = ",
					nNum,
					" and wiresectionid =(select id from wirestdsection where wiresection = ",
					dS,
					") and pipetypeid = (select id  from wireprotectionpipetype where pipetype='",
					pipeType,
					"')"
				});
				dPipeDia = DBhelper.GetReader(safeSql);
				return dPipeDia != -1.0;
			}
			return false;
		}

		public static bool GetSwitchSpec(string szType, double dIc, ref double strSpec)
		{
			string safeSql = string.Concat(new object[]
			{
				"select ie from switchspec where switchtypeid =(select id from switchtype where switchtype ='",
				szType,
				"') and ie > ",
				dIc,
				" order by ie asc limit 0,1"
			});
			strSpec = DBhelper.GetReader(safeSql);
			return strSpec != -1.0;
		}

		public static bool GetAllWireType(ref List<string> vecWireType)
		{
			vecWireType.Clear();
			string safeSql = "select wiretype from wiretype";
			vecWireType = DBhelper.GetReaderList(safeSql);
			return vecWireType != null && vecWireType.Count > 0;
		}

		public static bool GetWireTypeById(int nId, ref string strWireType)
		{
			string safeSql = "select wiretype from wiretype where id = " + nId;
			strWireType = DBhelper.GetReaderStr(safeSql);
			return strWireType.Trim() != "";
		}

		public static bool GetIdByWireType(string szWireType, ref int nId)
		{
			string safeSql = "select id from wiretype where wiretype ='" + szWireType + "'";
			bool result;
			try
			{
				nId = int.Parse(DBhelper.GetReaderStr(safeSql));
				if (nId != -1)
				{
					result = true;
				}
				else
				{
					result = false;
				}
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		public static bool GetWireNameByWireType(string szWireType, ref string strWireName)
		{
			string safeSql = "select wirename from wiretype where wiretype ='" + szWireType + "'";
			strWireName = DBhelper.GetReaderStr(safeSql);
			return strWireName.Trim() != "";
		}

		public static bool GetWireSById(int nId, ref double dS)
		{
			string safeSql = "select wiresection from wirestdsection where id = " + nId;
			dS = DBhelper.GetReader(safeSql);
			return dS != -1.0;
		}

		public static bool GetIdByWireS(double dS, ref int nId)
		{
			string safeSql = "select id from wirestdsection where wiresection = " + dS;
			bool result;
			try
			{
				nId = int.Parse(DBhelper.GetReaderStr(safeSql));
				if (dS != -1.0)
				{
					result = true;
				}
				else
				{
					result = false;
				}
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		public static bool GetAllPipeType(ref List<string> vecPipeType)
		{
			string safeSql = "select pipetype from wireprotectionpipetype";
			vecPipeType = DBhelper.GetReaderList(safeSql);
			return vecPipeType != null && vecPipeType.Count > 0;
		}

		public static bool GetPipeTypeById(int nId, ref string strPipeType)
		{
			string safeSql = "select pipetype from wireprotectionpipetype where id = " + nId;
			strPipeType = DBhelper.GetReaderStr(safeSql);
			return strPipeType != "";
		}

		public static bool GetIdByPipeType(string szPipeType, ref int nId)
		{
			string safeSql = "select id from wireprotectionpipetype where pipetype = '" + szPipeType + "'";
			bool result;
			try
			{
				nId = int.Parse(DBhelper.GetReaderStr(safeSql));
				if (nId != -1)
				{
					result = true;
				}
				else
				{
					result = false;
				}
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		public static bool GetPipeNameByPipeType(string szPipeType, ref string strPipeName)
		{
			string safeSql = "select pipename from wireprotectionpipetype where pipetype ='" + szPipeType + "'";
			strPipeName = DBhelper.GetReaderStr(safeSql);
			return strPipeName != "";
		}

		public static bool GetAllLayingMode(ref List<string> vecLayingMode)
		{
			string safeSql = "select mode from layingmode";
			vecLayingMode = DBhelper.GetReaderList(safeSql);
			return vecLayingMode != null && vecLayingMode.Count > 0;
		}

		public static bool GetLayingModeById(int nId, ref string strLayingMode)
		{
			string safeSql = "select mode from layingmode where id = " + nId;
			strLayingMode = DBhelper.GetReaderStr(safeSql);
			return strLayingMode != "";
		}

		public static bool GetIdByLayingMode(string szLayingMode, ref int nId)
		{
			string safeSql = "select id from layingmode where mode = '" + szLayingMode + "'";
			bool result;
			try
			{
				nId = int.Parse(DBhelper.GetReaderStr(safeSql));
				if (nId != -1)
				{
					result = true;
				}
				else
				{
					result = false;
				}
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		public static bool GetNoteByLayingMode(string szLayingMode, ref string strNote)
		{
			string safeSql = "select note from layingmode where mode = '" + szLayingMode + "'";
			strNote = DBhelper.GetReaderStr(safeSql);
			return strNote != "";
		}

		public static bool GetAllSwitchLocalName(ref List<string> vecLocalName)
		{
			string safeSql = "select localname from switchname";
			vecLocalName = DBhelper.GetReaderList(safeSql);
			return vecLocalName != null && vecLocalName.Count > 0;
		}

		public static bool GetSwitchNameByLocalName(string szLocalName, ref string szSwitchName)
		{
			string safeSql = "select name from switchname where localname = '" + szLocalName + "'";
			szSwitchName = DBhelper.GetReaderStr(safeSql);
			return szSwitchName != "";
		}

		public static bool GetAllSwitchType(ref List<string> vecSwitchType)
		{
			string safeSql = "select switchtype from switchtype";
			vecSwitchType = DBhelper.GetReaderList(safeSql);
			return vecSwitchType != null && vecSwitchType.Count > 0;
		}

		public static bool GetSwitchTypeById(int nId, ref string strSwitchType)
		{
			string safeSql = "select switchtype from switchtype where id= " + nId;
			strSwitchType = DBhelper.GetReaderStr(safeSql);
			return strSwitchType != "";
		}

		public static bool GetIdBySwitchType(string szSwitchType, ref int nId)
		{
			string safeSql = "select id from switchtype where switchtype = '" + szSwitchType + "'";
			bool result;
			try
			{
				nId = int.Parse(DBhelper.GetReaderStr(safeSql));
				if (nId != -1)
				{
					result = true;
				}
				else
				{
					result = false;
				}
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		public static bool IsSeparate(string szSwitchType)
		{
			return !(DBhelper.GetReaderStr("select separator from switchtype where switchtype ='" + szSwitchType + "'") == "False");
		}
	}
}
